package com.wash.shoes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.wash.shoes.domain.vo.ShoeVo;
import com.wash.shoes.entity.AccessoriesPo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.ShoePo;
import com.wash.shoes.mapper.AccessoriesMapper;
import com.wash.shoes.mapper.OrderMapper;
import com.wash.shoes.mapper.ShoeMapper;
import com.wash.shoes.mapper.UserShopMapper;
import com.wash.shoes.page.PageFactory;
import com.wash.shoes.request.ShoeQueryRequest;
import com.wash.shoes.service.ShoeService;
import com.wash.shoes.util.HeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 清洗得鞋子 服务实现类
 * </p>
 *
 * @author ljg
 * @since 2024-08-28
 */
@Service
public class ShoeServiceImpl extends ServiceImpl<ShoeMapper, ShoePo> implements ShoeService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AccessoriesMapper accessoriesMapper;

    @Autowired
    private UserShopMapper userShopMapper;

    @Override
    public Page<ShoeVo> pageList(ShoeQueryRequest shoePo) {
        Page page = null;
        if (shoePo.isNeedPage()) {
            page = PageFactory.defaultPage();
        }
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        // 获取用户绑定的类型,
        List<String> userBindTypeList = userShopMapper.getUserBindType(userId + "");
        String userBindType = null;
        if (!CollectionUtils.isEmpty(userBindTypeList)) {
            userBindType = userBindTypeList.get(0);
        }

        List<ShoeVo> dataList = baseMapper.pageList(page, shoePo, HeaderUtil.getShopIdStr(), userBindType);
        dataList.stream().forEach(shoeVo -> {
            OrderPo orderPo = orderMapper.selectById(shoeVo.getOrderId());
            List<AccessoriesPo> accessoriesPos = accessoriesMapper.selectList(new LambdaQueryWrapper<AccessoriesPo>().eq(AccessoriesPo::getOrderId, shoeVo.getOrderId()).eq(AccessoriesPo::getBizId, shoeVo.getId()));
            shoeVo.setWashFactory(orderPo.getWashFactory());
            shoeVo.setWashShop(orderPo.getWashShop());

            //转换填充配饰信息
            shoeVo.setAccessorVoList(accessoriesPos.stream().map(accessoriesPo -> {
                ShoePo.AccessorVo accessorVo = new ShoePo.AccessorVo();
                accessorVo.setType(accessoriesPo.getType());
                accessorVo.setWashCode(accessoriesPo.getWashCode());
                accessorVo.setStatus(accessoriesPo.getStatus());
                accessorVo.setId(accessoriesPo.getId());
                return accessorVo;
            }).collect(Collectors.toList()));
        });
        if (Objects.isNull(page)) {
            page = new Page();
        }
        page.setRecords(dataList);
        return page;
    }


    @Override
    public List<ShoePo> getByOrderId(String orderId) {
        return this.baseMapper.getByOrderId(orderId);
    }

    @Override
    public ShoeVo shoeDetailByWashCode(String washCode) {
        return this.baseMapper.shoeDetailByWashCode(washCode);
    }

    @Override
    public ShoeVo shoeDetailById(String id) {
        return this.baseMapper.shoeDetailById(id);
    }

    @Override
    public List<ShoePo> orderNotWashShoe(String orderId, String shoeId) {
        return this.baseMapper.orderNotWashShoe(orderId, shoeId);
    }

    @Override
    public List<ShoePo> getWashShoe() {
        return this.baseMapper.getWashShoe();
    }


}
